%% -*- coding: utf-8 -*-

%% @doc templ_env のテスト
%%
-module(t_templ_env).
-import(templ_env, [init/3,init_vtable/1,store_vtable/1,restore_vtable/0,
					put_value/3,get_by_key/2,
					set_delim/2,get_ldelim/0,get_rdelim/0]).

-include_lib("eunit/include/eunit.hrl").

vtable_test() ->
	VT0 = init_vtable([]),
	VT1 = put_value(VT0, "foo", "bar"),
	?assertMatch({ok,"bar"}, get_by_key(VT1, "foo")),
	VT2 = put_value(VT1, "foo", "hoge"),
	?assertMatch({ok,"hoge"}, get_by_key(VT2, "foo")).

vtable1_test() ->
	store_vtable(init_vtable([{"hoge", abc}, {"foo", "bar"}])), 
	VT = restore_vtable(),
	?assertMatch({ok,"bar"}, get_by_key(VT, "foo")),
	?assertMatch({ok,abc}, get_by_key(VT, "hoge")),
	store_vtable(init_vtable([{"hoge", 123}])),
	VT1 = restore_vtable(),
	?assertMatch({ok,123}, get_by_key(VT1, "hoge")),
	?assertMatch(error, get_by_key(VT1, "foo")).

binary_test() ->
	store_vtable(init_vtable([{<<"hoge">>, abc}, {<<"foo">>, <<"bar">>}])), 
	VT = restore_vtable(),
	?assertMatch({ok,<<"bar">>}, get_by_key(VT, "foo")),
	?assertMatch({ok,abc}, get_by_key(VT, "hoge")),
	store_vtable(init_vtable([{"hoge", 123}])),
	VT1 = restore_vtable(),
	?assertMatch({ok,123}, get_by_key(VT1, "hoge")),
	?assertMatch(error, get_by_key(VT1, "foo")).

delim_test() ->
	init([], "{", "}"),
	?assertMatch("{", get_ldelim()),
	?assertMatch("}", get_rdelim()),
	set_delim("---", "==="),
	?assertMatch("---", get_ldelim()),
	?assertMatch("===", get_rdelim()),
	set_delim("foo", "bar"),
	?assertMatch("foo", get_ldelim()),
	?assertMatch("bar", get_rdelim()).
